﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace SpectrumAnalyser.Common.Logs
{
    public class Logger
    {
        #region Console

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool AllocConsole();

        [DllImport("kernel32", SetLastError = true)]
        private static extern bool AttachConsole(int dwProcessId);

        #endregion

        public static void Log(string logMessage)
        {
            Process process = Process.GetCurrentProcess();

            if (process.ProcessName == "cmd")    //Is the uppermost window a cmd process?
            {
                Logger.AttachConsole(process.Id);
            }
            else
            {
                Logger.AllocConsole();
            }

            Console.WriteLine(string.Format("{0}: {1}", DateTime.Now, logMessage));
        }
    }
}